//
//  UIPopoverListView.m
//  UIPopoverListViewDemo
//
//  Created by su xinde on 13-3-13.
//  Copyright (c) 2013年 su xinde. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "UIPopoverListView.h"
#import "SDImageCache.h"
#import <QuartzCore/QuartzCore.h>
//#import "shopModel.h"
//#import "SDImageCache.h"
#define BackColor [UIColor colorWithRed:194/255.0 green:24/255.0 blue:30/255.0 alpha:1]
#define Button_H  30.0f

@interface UIPopoverListView ()
@property (nonatomic,strong)UIImageView *imageV;
@property (nonatomic,strong)UILabel *titleLable;
@property (nonatomic,strong)UILabel *contentLable;
@property (nonatomic,strong)UIButton *buttonAgain;
- (void)defalutInit;
- (void)fadeIn;
- (void)fadeOut;

@end

@implementation UIPopoverListView

//@synthesize datasource = _datasource;
//@synthesize delegate = _delegate;

//@synthesize listView = _listView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.picArray = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)defalutInit
{
    self.layer.cornerRadius = 10.0f;
    self.clipsToBounds = TRUE;
    self.backgroundColor = [UIColor clearColor];
    
    CGFloat xWidth = self.bounds.size.width;
    CGFloat xheight = self.bounds.size.height;
    
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, xWidth, xheight)];
    [self addSubview:bgImageView];
    //按钮
    _buttonView = [[UIButton alloc]initWithFrame:CGRectMake(xWidth-32,22, 25,25)];
    [_buttonView setImage:[UIImage imageNamed:@"yao_close"] forState:UIControlStateNormal];
    [_buttonView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_buttonView];

    //subView
    self.imageV.frame = CGRectMake(29,74, xWidth-58,114);
    
    [self addSubview:self.buttonAgain];
    self.buttonAgain.frame = CGRectMake((xWidth-96)/2,xheight-60, 96, 28);
    if (_issuccess ==YES) {
        [bgImageView setImage:[UIImage imageNamed:@"yao_pop_yes"]];
        [self addSubview:self.imageV];
    }else{
        [bgImageView setImage:[UIImage imageNamed:@"yao_pop_no"]];
        _buttonView.frame = CGRectMake(xWidth-32,8, 25,25);
        
        [self addSubview:self.titleLable];
        [self addSubview:self.contentLable];
        self.titleLable.frame = CGRectMake((xWidth-160)/2, 54, 160, 25);
        self.contentLable.frame = CGRectMake((xWidth-160)/2, 80, 160, 21);
    }
        
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];    
}
- (void)setTitle:(NSString *)title and:(BOOL)state
{
    _titleView.text = title;
    
    _issuccess = state;
    [self defalutInit];
}

-(void)setShopmodel:(shopModel *)shopmodel
{
//    _shopmodel = shopmodel;
//    [self defalutInit];

}

-(void)setImageURL:(NSString *)urlStr{
    UIImage *image = [UIImage imageNamed:@"placehold_hotnews"];
    [_imageV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:image];
}
- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,
                              keywindow.bounds.size.height/2.0f);
    [self fadeIn];
}

#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}

- (void)dismiss
{
    [self fadeOut];
}

-(void)ClickButtonOne{
    NSLog(@"oneone");
    [self fadeOut];
    
    if ([_delegate respondsToSelector:@selector(ShakeagainClickButton)]) {
        [_delegate ShakeagainClickButton];
    }
}
-(void)ClickButtonTwo{
    
    if ([_delegate respondsToSelector:@selector(ImmediateuseClickButton)]) {
        [_delegate ImmediateuseClickButton];
    }
    [self fadeOut];
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]init];
        _titleLable.textColor = [UIColor colorWithHexString:@"C92B2F"];
        _titleLable.text = [NSString stringWithFormat:@"很遗憾，没有中奖!"];
        _titleLable.font = [UIFont boldSystemFontOfSize:18];
        _titleLable.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLable;
}

-(UILabel *)contentLable{
    if (!_contentLable) {
        _contentLable = [[UILabel alloc]init];
        _contentLable.textColor = [UIColor colorWithHexString:@"737373"];
        _contentLable.text = [NSString stringWithFormat:@"换个姿势再试试!"];
        _contentLable.font = [UIFont systemFontOfSize:15];
        _contentLable.textAlignment = NSTextAlignmentCenter;
    }
    return _contentLable;
}

-(UIButton *)buttonAgain{
    if (!_buttonAgain) {
        _buttonAgain  = [[UIButton alloc]init];
        [_buttonAgain setImage:[UIImage imageNamed:@"again.png"] forState:UIControlStateNormal];
        [_buttonAgain addTarget:self action:@selector(ClickButtonOne) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonAgain;
}

-(UIImageView *)imageV{
    if (!_imageV) {
        _imageV  = [[UIImageView alloc]init];
    }
    return _imageV;
}

@end
